package com.sllp.C71S3TsllpShop.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.UserBizImpl;
import com.sllp.C71S3TsllpShop.util.RandomUtils;

@Controller
@SessionAttributes({"userResetTelCode","userResetEmailCode","userResetPwdCode","loginUser"})
public class SafeAction {
	
	@Resource
	private UserBizImpl ubi;
	@Resource
	private CarBizImpl cbiz;
	
	@GetMapping("Member_Safe.do") 
	public String memberSafe(HttpSession session,
			@SessionAttribute("btlist") List<List<BigType>> lists,Model m) {
		m.addAttribute("btlist", lists);
		return "Member_Safe";
	}
	
	/**
	 * 发送邮件
	 * @param usertel
	 * @return
	 */
	@Value ("${mail.formMail.addr}")//自己定义的配置参数
	private String from;
	@Value ("${mail.formMail.resetTelEmailContent}")//自己定义的配置参数
	private String contenttel;
	@Value ("${mail.formMail.resetEmEmailContent}")//自己定义的配置参数
	private String contentem;
	@Value ("${mail.formMail.resetPwdEmailContent}")//自己定义的配置参数
	private String contentpwd;
	
	//电话修改用 获取验证码
	@ResponseBody
	@PostMapping("Safe1.do")
	public Result toSendEmail1(@SessionAttribute("loginUser") User user,Model m) {
		int uid =1 ;
		String subrject = "请接收验证码";
		//获取验证码
		String code = RandomUtils.randomString(4);
		Result result= ubi.ToSendEmail21(subrject,user,from,contenttel,code);
		
		System.out.println("code :"+code);
		m.addAttribute("userResetTelCode", code);
		return result;
	}
	//邮箱修改用 获取验证码
	@ResponseBody
	@PostMapping("Safe2.do")
	public Result toSendEmail2(@SessionAttribute("loginUser") User user,Model m) {
		int uid =1 ;
		String subrject = "请接收验证码";
		//获取验证码
		String code = RandomUtils.randomString(4);
		Result result= ubi.ToSendEmail22(subrject,user,from,contentem,code);
		m.addAttribute("userResetEmailCode", code);
		return result;
	}
	
	//密码修改用 获取验证码
	@ResponseBody
	@PostMapping("Safe3.do")
	public Result toSendEmail3(@SessionAttribute("loginUser") User user,Model m) {
		int uid =1 ;
		String subrject = "请接收验证码";
		//获取验证码
		String code = RandomUtils.randomString(4);
		Result result= ubi.ToSendEmail23(subrject,user,from,contentpwd,code);
		
		System.out.println("code :"+code);
		m.addAttribute("userResetPwdCode", code);
		return result;
	}
	
	/*
	 * 修改手机号
	 */
	@ResponseBody
	@PostMapping("ModifytelByEmail.do")
	public Result toModifyTel(@SessionAttribute("loginUser") User user,
			String code,String tel2,@SessionAttribute("userResetTelCode") String userResetTelCode,Model m) {
		user.setTel(tel2);
		m.addAttribute("loginUser", user);
		if(code.equals(userResetTelCode)) {
			return	ubi.Modifytel(tel2,user);
		}else {
			return new Result(0,"验证失败");
		}
		
	}
	
	/**
	 * 修改Email
	 */
	@ResponseBody
	@PostMapping("ModifyEmailByEmail.do")
	public Result toModifyEmail(@SessionAttribute("loginUser") User user,
			String code,String email2,@SessionAttribute("userResetEmailCode") String userResetEmailCode,Model m) {
		user.setEmail(email2);
		m.addAttribute("loginUser", user);
	   System.out.println("code:"+code);
	   System.out.println("userResetEmailCode:"+userResetEmailCode);
		if(code.equals(userResetEmailCode)) {
			return	ubi.Modifyemail(email2,user);
		}else {
			return new Result(0,"验证失败");
		}
		
	}
	
	/**
	 * 修改pwd
	 */
	@ResponseBody
	@PostMapping("ModifyPwdByEmail.do")
	public Result toModifyPwd(@SessionAttribute("loginUser") User user,Model m,
			String code,String pwd2,@SessionAttribute("userResetPwdCode") String userResetPwdCode) {
		user.setPwd(pwd2);
		m.addAttribute("loginUser", user);
		if(code.equals(userResetPwdCode)) {
			return	ubi.Modifypwd(pwd2,user);
		}else {
			return new Result(0,"验证失败");
		}
		
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
